package vn.edu.ptithcm.pim.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import vn.edu.ptithcm.pim.dom.ResourceCategory;

@SuppressWarnings("rawtypes")
public class ResourceCategoryDao extends HibernateDaoSupport {

	public void save(ResourceCategory resourceCategory) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().save(resourceCategory);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void saveOrUpdate(ResourceCategory resourceCategory) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().saveOrUpdate(resourceCategory);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void lock(ResourceCategory resourceCategory) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().lock(resourceCategory, LockMode.NONE);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public void delete(ResourceCategory resourceCategory) {
		try {
			Transaction tx = getSession().beginTransaction();
			getHibernateTemplate().delete(resourceCategory);
			tx.commit();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ResourceCategory merge(ResourceCategory resourceCategory) {
		try {
			return (ResourceCategory) getHibernateTemplate().merge(
					resourceCategory);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public ResourceCategory findById(java.lang.Long id) {
		try {
			return (ResourceCategory) getHibernateTemplate().get(
					ResourceCategory.class, id);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List findByExample(ResourceCategory resourceCategory) {
		try {
			return getHibernateTemplate().findByExample("ResourceCategory",
					resourceCategory);
		} catch (RuntimeException re) {
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public List<ResourceCategory> findByProject(Long id) {
		try {
			Criteria c = getSession().createCriteria(ResourceCategory.class);
			c.add(Restrictions.eq("subjectId", id));
			c.add(Restrictions.eq("subjectType", "Project"));
			return c.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	public List getAll() {
		try {
			return getHibernateTemplate().loadAll(ResourceCategory.class);
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
